package com.hnucm.cooklife.presenter.home;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.Dish;
import com.hnucm.cooklife.bean.me.ToAddCollectData;
import com.hnucm.cooklife.contract.recom.Contract;
import com.hnucm.cooklife.model.RecomModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RecomPresenter extends BasePresenter<Contract.IRecomView> implements Contract.IRecomPresenter {
    Contract.IRecomModel mIRecomModel;

    public RecomPresenter() {
        mIRecomModel = new RecomModel();
    }

    @Override
    public void loadDish(int currentPage) {
        if(isViewAttached()){
            getView().onLoading();
        }
        mIRecomModel.loadDish(currentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Dish>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<Dish> dishes) {
                        if(isViewAttached()){
//                            System.out.println(dishes);
                            getView().onLoadDish(dishes);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if (isViewAttached()) {
//                            getView().onLoadFailed();
                            getView().onLoadingFailed();
                            System.out.println(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshDish(int currentPage) {
        mIRecomModel.refreshDish(currentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Dish>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<Dish> dishes) {
                        if(isViewAttached()){
                            getView().onRefreshDish(dishes);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if (isViewAttached()) {
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void collect(int userId, int dishId) {
        mIRecomModel.collect(userId, dishId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ToAddCollectData>() {
                    @Override
                    public void onSubscribe(@androidx.annotation.NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@androidx.annotation.NonNull ToAddCollectData collectData) {

                        if(isViewAttached()){
                            getView().onCollect(collectData,dishId);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@androidx.annotation.NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void unCollect(int userId, int dishId) {
        mIRecomModel.unCollcet(userId, dishId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ToAddCollectData>() {
                    @Override
                    public void onSubscribe(@androidx.annotation.NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@androidx.annotation.NonNull ToAddCollectData collectData) {
                        if(isViewAttached()){
                            getView().onUnCollect(collectData,dishId);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@androidx.annotation.NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
